//
//  Cerca.m
//  Grues
//
//  Created by Miquel Viladomat on 14/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Cerca.h"
#import "ZonaBlavaAppDelegate.h"

@implementation Cerca
@synthesize matriculacerc;

-(IBAction)cercar{
    
    NSString *urlString2 =[NSString stringWithFormat:@"http://www.giroassist.com/picimg/comprovamatricula.php?codi=%@",matriculacerc.text];
    NSMutableURLRequest *request2=[[[NSMutableURLRequest alloc] init] autorelease];
    [request2 setURL:[NSURL URLWithString:urlString2]];
    [request2 setHTTPMethod:@"POST"];    
    
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    dataCenter.matriculav=matriculacerc.text;
    
    conn=[[NSURLConnection alloc]initWithRequest:request2 delegate:self];
    if (conn) {
        webData = [[NSMutableData data] retain];
    }   
}
-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
    
}
-(void) connection:(NSURLConnection *) connection 
    didReceiveData:(NSData *) data {
    [webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection 
  didFailWithError:(NSError *) error {
    [webData release];
    [connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *theXML = [[NSString alloc] 
                        initWithBytes: [webData mutableBytes] 
                        length:[webData length] 
                        encoding:NSUTF8StringEncoding];
    
    if (xmlParser)
    {
        [xmlParser release];
    }    
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
    
    [theXML release];     
    
    [connection release];
    [webData release];
}
-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
  namespaceURI:(NSString *) namespaceURI 
 qualifiedName:(NSString *) qName
    attributes:(NSDictionary *) attributeDict {
    
    if( [elementName isEqualToString:@"Resposta"])
    {
        
            resposta = [[NSMutableString alloc] init];
        elementFound = YES;
    }
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    if (elementFound)
    {
        [resposta appendString: string];
        
    }
}
-(IBAction)dismiss{
    [self resignFirstResponder];
}
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
{
    //ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    if ([elementName isEqualToString:@"Resposta"])
    {
        if([resposta isEqualToString:@"si"]){
            
            
            NSLog(@"OK!!");
            iconaestat.image=[UIImage imageNamed:@"visto.png"];
        }if([resposta isEqualToString:@"no"]){
            
            
            NSLog(@"Noooooo!!");
            iconaestat.image=[UIImage imageNamed:@"negatiu.png"];
        }
    }
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
